<template>
  <div>
    <el-table :data="getSpecsList" border style="width: 100%">
      <el-table-column prop="id" label="规格编号" width="180"> </el-table-column>
      <el-table-column prop="specsname" label="规格名称" width="180"> </el-table-column>
      <el-table-column prop="address" label="规格属性">
        <template slot-scope="item">
          <div>
            <p v-for="item in item.row.attrs" :key="item">{{ item }}</p>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="状态">
        <template slot-scope="item">
          <div>
            <el-tag v-if="item.row.status == 1">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="item">
          <div>
            <el-button type="primary" size="mini" plain @click="edit(item.row.id)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" plain @click="del(item.row.id)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <!-- el-pagination 
    background 是否为分页按钮添加背景色
    layout 组件布局，子组件名用逗号分隔
    total	总条目数
    page-size	每页显示条目个数，支持 .sync 修饰符	number	—	10
    current-change	currentPage 改变时会触发
    -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="getCount"
      :page-size="getSize"
      @current-change="changePage"
    >
    </el-pagination>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { specsDelete } from "../../request/api.js";
export default {
  data() {
    return {};
  },
  methods: {
    // 辅助函数
    ...mapActions({
      getSpecsListActions: "specs/getSpecsListActions",
      getCountActions: "specs/getCountActions",
      changePageActions: "specs/changePageActions",
    }),
    // 编辑
    edit(id) {
      this.$emit("edit", id);
    },
    // 封装一个切换页码事件
    changePage(n) {
      // console.log(n);
      this.changePageActions(n);
    },
    // 封装一个删除事件
    del(id) {
      this.$confirm("你确定要删除我么", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          specsDelete({ id }).then((res) => {
            // console.log(res);
            this.$message.success(res.msg);
            // 重新调用所有数量和总页
            this.getSpecsListActions();
            this.getCountActions();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    this.getSpecsListActions();
    this.getCountActions();
  },
  watch: {},
  computed: {
    ...mapGetters({
      getSpecsList: "specs/getSpecsList",
      getCount: "specs/getCount",
      getSize: "specs/getSize",
    }),
  },
  filters: {},
  components: {},
};
</script>
<style scoped></style>
